package com.study.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Changes;
import com.study.model.pojo.Dept;
import com.study.model.pojo.Todo;
import com.study.services.ChangesServices;
import com.study.services.DeptServices;
import com.study.services.EmpServices;
import com.study.services.TodoServices;

@RestController
@CrossOrigin
public class DeptController {
	
	@Autowired
	DeptServices ds;
	@Autowired
	EmpServices es;
	@Autowired
	ChangesServices cs;
	@Autowired
	TodoServices tds;
	

	/**
	 * 新增部门*/
	@RequestMapping("/moredept")
	public JSONObject moredept(Dept d) {
		System.out.println("新增部门");
		System.out.println("部门编号："+d.getDeptno());
		ds.addDept(d);
		System.out.println("部门编号2："+d.getDeptno());
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 删除部门*/
	@RequestMapping("/removedept")
	public JSONObject removedept(int id) {
		ds.delDept(id);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 部门添加经理*/
	@RequestMapping("/putinmanager")
	public JSONObject putinmanager(Integer eid,int deptno) {
		System.out.println("进入了putinmanager");
		Map m=new HashMap();
		m.put("eid",eid);
		m.put("deptno",deptno);
		ds.addManager(m);
		
		System.out.println("进入了putinmanager2");
		Map m2 =new HashMap();
		m2.put("eid",eid);
		m2.put("deptno",deptno);
		es.updatedeptByid(m2);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 显示不是当前部门的部门*//*
	@RequestMapping("/notnowdept")
	public JSONArray notnowdept(int eid) {
		JSONArray json = new JSONArray();
		List<Dept> list=ds.findnotnow(eid);
		for(Dept d: list){
            JSONObject jo = new JSONObject();
            jo.put("deptno", d.getDeptno());
            jo.put("danme", d.getDname());
            jo.put("eid", d.getEid());
            json.add(jo);
        }
		return json;
	}*/
	/**
	 * 加载调职页面*/
	@RequestMapping("/showtransfer")
	public JSONArray showtransfer() {
		JSONArray json = new JSONArray();
		List<Dept> list=ds.findAll();
		for(Dept d: list){
            JSONObject jo = new JSONObject();
            jo.put("deptno", d.getDeptno());
            jo.put("dname", d.getDname());
            jo.put("eid", d.getEid());
            json.add(jo);
        }
		return json;
	}
	
	/**
	 * 提交调动表*/
	@RequestMapping("/transfer")
	public JSONObject transfer(Changes c) {
		System.out.println("提交调动表");
		cs.addChanges(c);
		JSONObject json = new JSONObject();
		int leadid=es.findMng(c.getEid());
		Todo to=tds.queryTDmngandtype(leadid,25);
		System.out.println(to);
		if(to==null){
			Todo t=new Todo();
			t.setType(25);
			t.setNum(1);
			t.setExplains("办理跨部门调职");
			t.setEid(es.findMng(c.getEid()));
			t.setPath("/home/transdepart");
			tds.addTodo(t);
		}else{
			tds.addTodonum(to.getToid());
		}
		json.put("ans", "y");
		return json;
	}
	
}
